package com.aiyige.page.login;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.aiyige.MyApp;
import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.Country;
import com.aiyige.model.request.CountryRequest;
import com.aiyige.page.login.country.CountryAdapter;
import com.aiyige.utils.StringUtils;
import com.aiyige.utils.UIHelper;
import com.aiyige.utils.widget.WaveSideBar;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.vondear.rxtools.RxDataTool;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.OnClick;
import qdx.stickyheaderdecoration.NormalDecoration;

/**
 * 选择国家
 */
@Route(path = ARouterConfig.SelectCountryPage)
public class SelectCountryPage extends BaseActivity {
    public static final String TAG = SelectCountryPage.class.getSimpleName();

    public static final int COUNTRY_REQUEST_CODE = 86;

    private RecyclerView rvContacts;
    private WaveSideBar sideBar;
    CountryAdapter countryAdapter;

    private List<Country> countryList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_selectcounty);
        ButterKnife.bind(this);
        initData();
        init();
    }


    private void init() {
        if (countryList == null || countryList.isEmpty()) {
            return;
        }
        final NormalDecoration decoration = new NormalDecoration() {
            @Override
            public String getHeaderName(int pos) {
                return countryList.get(pos).getIndex();
            }
        };
        decoration.setHeaderHeight(UIHelper.dip2px(this,25));
        decoration.setTextSize(UIHelper.sp2px(this,11));
        decoration.setTextColor(ContextCompat.getColor(this,R.color.zwcolor_black_111111));
        decoration.setTextPaddingLeft(UIHelper.dip2px(this,15));
        decoration.setHeaderContentColor(Color.parseColor("#f5f5f5"));
        rvContacts = (RecyclerView) findViewById(R.id.rv_country);
        rvContacts.addItemDecoration(decoration);
        rvContacts.setLayoutManager(new LinearLayoutManager(this));
        countryAdapter = new CountryAdapter(countryList, R.layout.item_country);
        rvContacts.setAdapter(countryAdapter);
        sideBar = (WaveSideBar) findViewById(R.id.side_bar);
//        sideBar.setMaxOffset(0);
        sideBar.setOnSelectIndexItemListener(new WaveSideBar.OnSelectIndexItemListener() {
            @Override
            public void onSelectIndexItem(String index) {
                for (int i=0; i<countryList.size(); i++) {
                    if (countryList.get(i).getIndex().equals(index)) {
                        ((LinearLayoutManager) rvContacts.getLayoutManager()).scrollToPositionWithOffset(i, 0);
                        return;
                    }
                }
            }
        });
        countryAdapter.setOnItemClickListener(new CountryAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Country country) {
                Intent intent = new Intent();
                intent.putExtra("country",country);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    private void initData() {
        String locale = Locale.getDefault().getLanguage();
        boolean isChina;
        if (isEmpty(locale) || locale.contains("zh") || locale.contains("CN")) {
            isChina = true;
        } else {
            isChina = false;
        }
        InputStream[] certInputStream = new InputStream[1];
        try {
            certInputStream[0] = MyApp.getAppContext().getAssets().open(isChina?"chineseCountryJson.txt":"englishCountryJson.txt");
            String countryJson = null;
            if (certInputStream[0] != null) {
                countryJson = RxDataTool.inputStream2String(certInputStream[0],"utf-8");
            }
            CountryRequest countryRequest = null;
            if (!isEmpty(countryJson)) {
                countryJson = StringUtils.replaceBlank(countryJson);
                countryRequest = JsonUtil.toObject(countryJson,CountryRequest.class);
            }
            if (countryRequest != null && countryRequest.getData() != null) {
                countryList = countryRequest.getData();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @OnClick(R.id.titleBackBtn)
    public void onViewClicked() {
        finish();
    }
}
